package com.wf.smartcomm.server.client.action;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ModelDriven;
import com.wf.smartcomm.server.bean.MerchantUser;
import com.wf.smartcomm.server.bean.Scores;
import com.wf.smartcomm.server.bean.Terminal;
import com.wf.smartcomm.server.bean.User;
import com.wf.smartcomm.server.response.ResponseCode;
import com.wf.smartcomm.server.response.ResponseMessage;
import com.wf.smartcomm.server.service.MerchantService;
import com.wf.smartcomm.server.service.MerchantUserService;
import com.wf.smartcomm.server.service.ScoreService;
import com.wf.smartcomm.server.service.TerminalService;
import com.wf.smartcomm.server.service.UserService;
import com.wf.smartcomm.server.util.IdUtils;

@Controller
@Scope("prototype")
public class ScoreAction extends BaseAction implements ModelDriven<MerchantUser>{
	
	private MerchantUser merchantUser = new MerchantUser();
	
	@Resource
	private ScoreService scoreService;
	
	@Resource
	private UserService userService;
	
	@Resource
	private MerchantService merchantService;
	
	@Resource
	private MerchantUserService merchantUserService;
	
	@Resource
	private TerminalService terminalService;
	
	public void save()throws Exception{
		String json = request.getParameter("json");
		ObjectMapper objectMapper = new ObjectMapper();
		List<Scores> scores = objectMapper.readValue(json, new TypeReference<List<Scores>>(){});
		List<String> notExist = new ArrayList<String>();
		for (Scores scores2 : scores) {
			User customer = userService.getUserByUsername(scores2.getCustomer().getUsername());
			if (customer == null) {
				notExist.add(scores2.getObjectId());
				continue;
			}
			//scores2.setObjectId(IdUtils.uuid());
			scores2.setCustomer(customer);
			scoreService.save(scores2);
		}
		
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("code", ResponseCode.OK);
		map.put("data", notExist);
		printJsonData(gson.toJson(map));
	}
	
	public void getScale()throws Exception{
		HashMap<String, Object> map = new HashMap<String, Object>();
		Terminal terminal = terminalService.get(merchantUser.getObjectId());
		map.put("code", ResponseCode.OK);
		map.put("data",terminal);
		printJsonData(gson.toJson(map));
	}
	
	public void receipt()throws Exception{
		HashMap<String, Object> map = new HashMap<String, Object>();
		Terminal terminal = terminalService.get(merchantUser.getObjectId());
		terminal.setStatus(terminal.getStatus()+1);
		terminalService.update(terminal);
		map.put("code", ResponseCode.OK);
		printJsonData(gson.toJson(map));
	}
	
	public void login()throws Exception{
		HashMap<String, Object> map = new HashMap<String, Object>();
		MerchantUser loginUser = merchantUserService.login(merchantUser.getUsername(), merchantUser.getPassword());
		if (loginUser == null) {
			map.put("code", ResponseCode.USERNAME_OR_PASSWORD_WRONG);
			map.put("data", ResponseMessage.USERNAME_OR_PASSWORD_WRONG);
		}else {
			Terminal terminal = terminalService.getByToken(merchantUser.getTerminalToken());
			if (terminal == null) {
				terminal = new Terminal();
				terminal.setObjectId(IdUtils.uuid());
				terminal.setPushId(IdUtils.uuid16());
				terminal.setOnline(1);
				terminal.setScale(loginUser.getScale());
				terminal.setMerchant(loginUser.getMerchant());
				terminal.setToken(merchantUser.getTerminalToken());
				terminal.setStatus(0);
				terminalService.save(terminal);
			}
			map.put("code", ResponseCode.OK);
			map.put("data",terminal);
		}
		printJsonData(gson.toJson(map));
	}

	@Override
	public MerchantUser getModel() {
		return merchantUser;
	}

}
